package com.base.api.pts.impl;

import com.alibaba.fastjson.JSONObject;
import com.base.api.pts.PtsService;
import com.base.dao.pts.PtsNurseShiftMapper;
import com.base.dao.pts.PtsNurseShiftTimeMapper;
import com.base.vo.pts.PtsNurseShift;
import com.base.vo.pts.PtsNurseShiftTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wnhuang
 * @create 2020-09-16 19:15
 */
@Service
public class PtsServiceImpl implements PtsService {

    @Autowired
    PtsNurseShiftMapper nurseShiftMapper;

    @Autowired
    PtsNurseShiftTimeMapper nurseShiftTimeMapper;

    /**
     * 获取排班信息
     *
     * @param startDate 开始日期
     * @param orgCode   科室编码
     * @param isCopy   复制上周
     * @return
     */
    @Override
    public String getNurseShiftData(String startDate, String orgCode,String isCopy) {
        Map<String, Object> map = new HashMap<>();
        List<PtsNurseShift> list = nurseShiftMapper.selectByStartDateAndOrgCode(startDate, orgCode);
        if("1".equals(isCopy)){
            for (int i = 0; i < list.size(); i++) {
                list.get(i).setId(null);
            }
        }
        map.put("code", "0");
        map.put("data", list);
        return JSONObject.toJSONString(map);
    }

    /**
     * 获取当前科室配置的排班列表
     *
     * @param orgCode
     * @return
     */
    @Override
    public String getShiftTimeByOrgCode(String orgCode) {
        Map<String, Object> map = new HashMap<>();
        List<PtsNurseShiftTime> list = nurseShiftTimeMapper.selectByOrgCode(orgCode);
        map.put("code", "0");
        map.put("data", list);
        return JSONObject.toJSONString(map);
    }

    /**
     * 保存排班信息
     *
     * @param list
     * @return
     */
    @Override
    public String saveNurseShiftData(List<PtsNurseShift> list) {
        Map<String, Object> map = new HashMap<>();
        if (!list.isEmpty()) {
            List<PtsNurseShift> list1 = new ArrayList<>();
            List<PtsNurseShift> list2 = new ArrayList<>();
            for (PtsNurseShift ptsNurseShift : list) {
                if (ptsNurseShift.getId() == null) {
                    list1.add(ptsNurseShift);
                } else {
                    list2.add(ptsNurseShift);
                }
            }
            if (!list1.isEmpty()) {
                nurseShiftMapper.insertBatch(list1);
            }
            if (!list2.isEmpty()) {
                for (PtsNurseShift ptsNurseShift : list2) {
                    nurseShiftMapper.updateByPrimaryKey(ptsNurseShift);
                }
            }
        }
        map.put("code", "0");
        return JSONObject.toJSONString(map);
    }

    /**
     * 添加科室与排班
     *
     * @param list
     * @return
     */
    @Override
    public String saveNurseShiftTime(List<PtsNurseShiftTime> list) {
        Map<String, Object> map = new HashMap<>();
        if (!list.isEmpty()) {
            nurseShiftTimeMapper.insertBatch(list);
        }
        map.put("code", "0");
        return JSONObject.toJSONString(map);
    }
}
